Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Displaying favico image from database to html using javascript shows "undefined"

I have read a lot of similar examples here on S.O yet, I cannot figure out what is the problem. The item.picture image is successfully displayed on the html and its url can also be printed on the console.

However, the favico displays undefined both on the console and on html.
Here is the sample code:

$(function () {
  jQuery.ajax({
    type:'GET',
    url:"api/wedding/?format=json&limit=20",
    dataType:"json",
    success :function (data) {
      var listA=$("#wedding_id");
      for(var i=0;i<data.results.length;i++) {
        var  item=newDiv(data.results[i])
        listA.append(item);
      }
    },
    error:function (e) {
      console.log(e) // Better for debugging
      // alert("Error" + e)
    }
  });
});

function newDiv(item)
  {
  if (item.pic) {
    var wed_pic = item.pic.split(",")[0];
  }
  if (item.favico) {
    var favicon = item.favico.split("/")[0]; 
  }
  console.log(item.pic); // <--- can display the pic_url on console
  console.log(favicon); //shows  'undefined'
 
  var template = '<ul class="wedding-list" id='+ item.wedding_id +'>'
               + '<a class="wedding-icon" href="#">' + favicon + '</a>'  //<--- where I think lies the problem
               + '<a class="wed_img" target="_blank" href=' + item.url + '>' 
               + '<img src=' + wed_pic + '>' 
               + '</a> </ul>' +
  return template
  }

What could possibly be the cause of the undefined error? Thank you for helping.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

for a start

const listA = document.querySelector('#wedding_id')
  ;
fetch ('api/wedding/?format=json&limit')
  .then (res => res.json())
  .then (data =>
    {
    for (let row of data.results)
      listA.appendChild( newDiv(row))  
    })
  .catch (err => console.error(err)) 
  ; 
function newDiv(item)
  {
  let
    e_UL    = document.createElement('ul')
  , wed_pic = !!item?.pic    ? item.pic.split(',')[0]    : ''
  , favicon = !!item?.favico ? item.favico.split('/')[0] : ''
    ;
  console.log('item ----->', JSON.stringify(item,0,2) )
  console.log('wed_pic  ->', wed_pic  )
  console.log('favicon  ->', favicon  ) 
  
  e_UL.id        = item.wedding_id
  e_UL.className = 'wedding-list'
  e_UL.innerHTML = ` 
    <a class="wedding-icon" href="#"> ${favicon} </a> 
    <a class="wed_img" target="_blank" href="${item.url}">
      <img src="${wed_pic}">
    </a>`
    
  return e_UL
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!